<template>
    <el-container>
        <el-main class="wrapper">
            <section class="panel cms-content">
                <div class="panel-body">
                    <div class="cms-btn-group">
                        <el-button type="warning" icon="fa fa-refresh" @click="list()" size="small"> 刷新</el-button>
                        <el-button type="danger" icon="fa fa-trash" @click="multiVerify()" size="small"> 审核</el-button>
                        <el-button type="info" icon="fa fa-trash" @click="multiDelete()" size="small"> 取消</el-button>
                    </div>
                    <div class="cms-search-group">
                        <div class="cms-search-item">
                            <el-input clearable size="medium" v-model="params.kwd" placeholder="手机号或姓名或身份证" class="input-with-select">
                                <el-button type="primary" @click="search()" slot="append" icon="el-icon-search"></el-button>
                            </el-input>
                        </div>
                    </div>
                    <el-table v-loading="loading"
                              element-loading-text="努力加载中..."
                              element-loading-spinner="el-icon-loading"
                              @selection-change="handleSelectionChange"
                              tooltip-effect="dark" :data="listData.data" border>
                        <el-table-column align="center" type="selection" width="30"></el-table-column>
                        <el-table-column align="center" prop="id" label="编号" width="60"></el-table-column>
                        <el-table-column prop="mobile" label="手机号" width="150"></el-table-column>
                        <el-table-column prop="truename" label="真实姓名" width="150"></el-table-column>
                        <el-table-column prop="card" label="身份证号码" width="250"></el-table-column>
                        <el-table-column align="center" prop="column_text" label="状态" width="120">
                            <template slot-scope="scope">
                                <span v-html="scope.row.column_text"></span>
                            </template>
                        </el-table-column>
                        <el-table-column align="center" prop="position_text" label="推荐" width="75">
                            <template slot-scope="scope">
                                <span v-html="scope.row.position_text"></span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="apply_at" label="申请时间" :show-overflow-tooltip="true" width="160"></el-table-column>
                        <el-table-column label="操作" >
                            <template slot-scope="scope">
                                <el-button @click="edit(scope.row)" icon="fa fa-edit" type="primary" size="mini" plain> 编辑</el-button>
                                <el-button v-if="scope.row.column == 1" @click="verify(scope.row)" icon="fa fa-edit" type="danger" size="mini" plain> 审核</el-button>
                                <el-button type="warning" icon="fa fa-download" @click="link(scope.row.id)" size="small" plain> 下载</el-button>
                                <el-button @click="destroy(scope.row.id)" icon="fa fa-trash" type="info" size="mini" plain> 取消</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <pagination :pageData="listData" :params="params" @list="list"/>
                </div>
            </section>
        </el-main>
        <!-- 表单弹窗-->
        <el-dialog
                v-if="dialog.show"
                :title="dialog.title"
                custom-class="cms-dialog"
                :top="dialog.top"
                :visible.sync="dialog.show"
                :width="dialog.width">
            <el-form :model="form" :rules="rules" ref="form" label-width="120px">
                <el-form-item label="手机号：" :rules="[{ required: true, message: '请填写用户手机号', trigger: 'blur'}]" prop="mobile">
                    <el-input  v-model="form.mobile" readonly placeholder="请填写用户手机号"></el-input>
                </el-form-item>
                <el-form-item label="真实姓名：" :rules="[{ required: true, message: '请填写真实姓名', trigger: 'blur'}]" prop="truename">
                    <el-input v-model="form.truename" placeholder="请填写真实姓名"></el-input>
                </el-form-item>
                <el-form-item label="身份证号：" :rules="[{ required: true, message: '请填写身份证号', trigger: 'blur'}]" prop="card">
                    <el-input v-model="form.card" placeholder="请填写身份证号"></el-input>
                </el-form-item>
                <el-form-item required label="状态：" prop="column">
                    <el-radio-group v-model="form.column">
                        <el-radio border :label="1">待审核</el-radio>
                        <el-radio border :label="2">已审核</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item required label="推荐：" prop="position">
                    <el-radio-group v-model="form.position">
                        <el-radio border :label="1">正常</el-radio>
                        <el-radio border :label="2">推荐</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialog.show = false">取 消</el-button>
                <el-button type="primary" @click="update('form')">确 定</el-button>
              </span>
        </el-dialog>
    </el-container>
</template>

<script>
    import {mapGetters} from 'vuex';
    import {UserColumn,UserVerify,UserRefuse,UserUpdate} from '@/utils/request';
    import Pagination from '@/components/pagination';
    export default {
        components: {Pagination},
        data() {
            return {
                listData: [],//列表数据
                params: {page:1,kwd:''},
                dialog: {
                    title: '',
                    top: '10vh',
                    show: false,
                    width: '550px'
                },
                multipleSelection:[],//多选项
                form:null,
            }
        },
        activated(){
            this.list();
        },
        computed: {
            ...mapGetters(['active','loading','upload']),
        },
        methods: {
            link(id){
                window.location.href = 'http://liange.bh/api/download?id='+id;
            },
            //编辑
            edit(info){
                this.form = {
                    id: info.id,
                    truename: info.truename,
                    mobile: info.mobile,
                    card: info.card,
                    column: info.column,
                    position: info.position
                };
                this.dialog.title = '编辑专栏';
                this.dialog.show = true;
            },
            //上传封面
            uploadSuccess(res, file) {
                this.form.avatar = res.data.path;
            },
            uploadBefore(file) {
                const isJPG = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/gif';
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isJPG) {
                    this.$message.error('上传封面图片只能是 JPG | PNG | GIF格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传封面图片大小不能超过 2M!');
                }
                return isJPG && isLt2M;
            },
            //筛选
            search(){
                this.params.page = 1;
                this.list();
            },
            //列表
            list(){
                this.$store.dispatch('setLoading', true);
                UserColumn({params: this.params}).then((res)=>{
                    if (res.code) {
                        this.$message.error(res.msg);
                    } else {
                        this.listData = res.data;
                    }
                    this.$store.dispatch('setLoading', false);
                })
            },
            //更新
            update(formName){
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        UserUpdate(this.form).then((res)=>{
                            if(res.code){
                                this.$message.error(res.msg);
                            }else{
                                this.$message({
                                    type: 'success',
                                    duration: '1000',
                                    message: res.msg,
                                    onClose:() =>{
                                        this.dialog.show = false;
                                        this.list();
                                    }
                                });
                            }
                        });
                    }
                });
            },
            // 多选删除
            multiDelete(){
                if(this.multipleSelection.length == 0){
                    this.$message.error('请至少选择一项你要删除的项');return false;
                }
                this.destroy(this.multipleSelection);
            },
            multiVerify(){
                if(this.multipleSelection.length == 0){
                    this.$message.error('请至少选择一项你要审核的项');return false;
                }
                this.verify(this.multipleSelection);
            },
            handleSelectionChange(val) {
                let select = [];
                if(val.length > 0){
                    val.filter((item)=>{
                        if(select.indexOf(item.id) === -1){
                            select.push(item.id)
                        }
                    });
                    this.multipleSelection = select;
                }else{
                    this.multipleSelection = [];
                }
            },
            verify(id){
                UserVerify({id: id}).then((res) => {
                    if (res.code) {
                        this.$message.error(res.msg);
                    } else {
                        this.list();
                    }
                });
            },
            //删除
            destroy(id){
                this.$confirm('确定要取消选中的专栏信息吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    UserRefuse({id: id}).then((res) => {
                        if (res.code) {
                            this.$message.error(res.msg);
                        } else {
                            this.list();
                        }
                    });
                }).catch(() => {});
            },
        }
    }
</script>

<style scoped>

</style>
